//
//  BuaatRSA.m
//  MemberProject
//
//  Created by mac on 15/7/15.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "BuaatRSA.h"
#include <openssl/rsa.h>
#include <openssl/pem.h>
@implementation BuaatRSA{
    NSString *_publicKey;
    NSString *_privateKey;
    RSA *_publicKeyRSA;
    RSA *_privateKeyRSA;
}

- (id)init {
    self = [super init];
    if (self) {
        self.padding = RSA_PKCS1_PADDING;
    }
    return self;
}

- (void)setPublicKey:(NSString *)publicKey {
    NSMutableString *publicKeyArr=[[NSMutableString alloc]init];
    [publicKeyArr appendFormat:@"-----BEGIN PUBLIC KEY-----\n"];
    [publicKeyArr appendFormat:@"%@\n",[publicKey substringWithRange:NSMakeRange(0, 64)]];
    [publicKeyArr appendFormat:@"%@\n",[publicKey substringWithRange:NSMakeRange(64, 64)]];
    [publicKeyArr appendFormat:@"%@\n",[publicKey substringWithRange:NSMakeRange(64+64, 64)]];
    [publicKeyArr appendFormat:@"%@\n",[publicKey substringWithRange:NSMakeRange(64+64+64, 24)]];
    [publicKeyArr appendFormat:@"-----END PUBLIC KEY-----"];
    
    
    _publicKey=publicKeyArr;
    const char *key = [_publicKey UTF8String];
    BIO *bio = BIO_new_mem_buf((void *) key, (int) strlen(key));
    _publicKeyRSA = PEM_read_bio_RSA_PUBKEY(bio, NULL, 0, NULL);
    BIO_free(bio);
    
}

- (void)setPrivateKey:(NSString *)privateKey {
    _privateKey = privateKey;
    
    const char *key = [privateKey UTF8String];
    BIO *bio = BIO_new_mem_buf((void *) key, (int) strlen(key));
    _privateKeyRSA = PEM_read_bio_RSAPrivateKey(bio, NULL, 0, NULL);
    BIO_free(bio);
}

- (NSString *)encryptStrWithPublicKey:(NSString *)str {
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    int maxSize = RSA_size(_publicKeyRSA);
    unsigned char *output = (unsigned char *) malloc(maxSize * sizeof(char));
    self.padding = RSA_PKCS1_PADDING;
    int bytes = RSA_public_encrypt([data length], [data bytes], output, _publicKeyRSA, _padding);
    if(bytes>0){
        NSData *outData=[NSData dataWithBytes:output length:(NSUInteger) bytes];
        outData = [outData base64EncodedDataWithOptions:0];
        NSString *outStr = [[NSString alloc] initWithData:outData encoding:NSUTF8StringEncoding];
        return outStr;
    }else{
        return nil;
    }
}

- (NSString *)encryptStrWithPrivateKey:(NSString *)str {
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    int maxSize = RSA_size(_privateKeyRSA);
    unsigned char *output = (unsigned char *) malloc(maxSize * sizeof(char));
    int bytes = RSA_private_encrypt([data length], [data bytes], output, _privateKeyRSA, _padding);
    
    if(bytes>0){
        NSData *outData=[NSData dataWithBytes:output length:(NSUInteger) bytes];
        outData = [outData base64EncodedDataWithOptions:0];
        NSString *outStr = [[NSString alloc] initWithData:outData encoding:NSUTF8StringEncoding];
        return outStr;
    }else{
        return nil;
    }
}

- (NSString *)decryptStrWithPublicKey:(NSString *)str {
    NSData *data=[[NSData alloc] initWithBase64EncodedString:str options:NSDataBase64DecodingIgnoreUnknownCharacters];
    int maxSize = RSA_size(_publicKeyRSA);
    unsigned char *output = (unsigned char *) malloc(maxSize * sizeof(char));
    int bytes = RSA_public_decrypt([data length], [data bytes], output, _publicKeyRSA, _padding);
    if(bytes>0){
        NSData *outData=[NSData dataWithBytes:output length:(NSUInteger) bytes];
        NSString *outStr = [[NSString alloc] initWithData:outData encoding:NSUTF8StringEncoding];
        return outStr;
        
    }else{
        return nil;
    }
}

- (NSString *)decryptStrWithPrivateKey:(NSString *)str {
    NSData *data=[[NSData alloc] initWithBase64EncodedString:str options:NSDataBase64DecodingIgnoreUnknownCharacters];
    int maxSize = RSA_size(_privateKeyRSA);
    unsigned char *output = (unsigned char *) malloc(maxSize * sizeof(char));
    int bytes = RSA_private_decrypt([data length], [data bytes], output, _privateKeyRSA, _padding);
    if(bytes>0){
        NSData *outData=[NSData dataWithBytes:output length:(NSUInteger) bytes];
        NSString *outStr = [[NSString alloc] initWithData:outData encoding:NSUTF8StringEncoding];
        return outStr;
        
    }else{
        return nil;
    }
}



@end
